More pulling at removing C strings internally: unicsv_print_str and strenquote.
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 15 Sep 2013 18:44:12 +0000 (18:44 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 15 Sep 2013 18:44:12 +0000 (18:44 +0000)
A victim of this is the seemingly unused sportsim format. It was doing
stupid things in the format definition with high byte characters and
unclear encodings.  After a few hours trying to save it, I just chopped it.

git-svn-id: http://gpsbabel.googlecode.com/svn/trunk@4614 f51c46e8-681c-474f-0cfe-069cfd0219fb

gpsbabel/csv_util.cc
gpsbabel/defs.h
gpsbabel/style/deprecated/sportsim.style [new file with mode: 0644]
gpsbabel/style/sportsim.style [deleted file]
gpsbabel/testo.d/deprecated/sportsim.test [new file with mode: 0644]
gpsbabel/testo.d/sportsim.test [deleted file]
gpsbabel/unicsv.cc
gpsbabel/util.cc
gpsbabel/xcsv_tokens.gperf
gpsbabel/xol.cc

index 246cb1f59b0efcbb0305fe3dda317675088b8134..a09c577a007960710a6be57f883d451eba690b83 100644 (file)
@@ -1531,7 +1531,6 @@ xcsv_data_read(void)
         ;
       }
     }
-
   }
 }
 
@@ -2210,7 +2209,7 @@ xcsv_data_write(void)
   ogue_t* ogp;
   time_t time;
   struct tm tm;
-  char tbuf[32];
+//  char tbuf[32];
 
   /* reset the index counter */
   waypt_out_count = 0;
@@ -2224,44 +2223,30 @@ xcsv_data_write(void)
 
   /* output prologue lines, if any. */
   QUEUE_FOR_EACH(&xcsv_file.prologue, elem, tmp) {
-    char* cout, *ctmp;
     ogp = (ogue_t*) elem;
+    // If the XCSV description contains weird characters (like sportsim)
+    // this is where they get lost.
+    QString cout = ogp->val;
 
-    cout = xstrdup((ogp->val) ? ogp->val : "");
+    // Don't do potentially expensive replacements if token prefix 
+    // isn't present;
+    if (cout.contains("__")) {
+      cout.replace("__FILE__", xcsv_file.fname);
+      cout.replace("__VERSION__", time == 0 ? "" : gpsbabel_version);
 
-    while ((ctmp = strsub(cout, "__FILE__", xcsv_file.fname))) {
-      xfree(cout);
-      cout = ctmp;
-    }
+      QDateTime dt = QDateTime::fromTime_t(time);
+      dt = dt.toTimeSpec(Qt::UTC);
 
-    while ((ctmp = strsub(cout, "__VERSION__", (time == 0) ? "" : gpsbabel_version))) {
-      xfree(cout);
-      cout = ctmp;
-    }
+      QString dts = dt.toString("ddd MMM dd hh:mm:ss yyyy");
+      cout.replace("__DATE_AND_TIME__", dts);
 
-    while (strstr(cout, "__DATE__")) {
-      strftime(tbuf, sizeof(tbuf), "%m/%d/%Y", &tm);
-      ctmp = strsub(cout, "__DATE__", tbuf);
-      xfree(cout);
-      cout = ctmp;
-    }
+      QString d = dt.toString("MM/dd/yyyy");
+      cout.replace("__DATE__", d);
 
-    while (strstr(cout, "__TIME__")) {
-      strftime(tbuf, sizeof(tbuf), "%H:%S:%M", &tm);
-      ctmp = strsub(cout, "__TIME__", tbuf);
-      xfree(cout);
-      cout = ctmp;
+      QString t = dt.toString("hh:mm:ss");
+      cout.replace("__TIME__", t);
     }
-
-    while (strstr(cout, "__DATE_AND_TIME__")) {
-      strftime(tbuf, sizeof(tbuf), "%a %b %d %H:%M:%S %Y", &tm);
-      ctmp = strsub(cout, "__DATE_AND_TIME__", tbuf);
-      xfree(cout);
-      cout = ctmp;
-    }
-
-    gbfprintf(xcsv_file.xcsvfp, "%s", cout);
-    xfree(cout);
+    gbfprintf(xcsv_file.xcsvfp, "%s", CSTR(cout));
     gbfprintf(xcsv_file.xcsvfp, "%s", xcsv_file.record_delimiter);
   }
 
index cb439da160f6abc79ceaacd27a3704196c7ea150..020ee16785aa46e0b8a094cdde80575fe652bc20 100644 (file)
@@ -1028,7 +1028,7 @@ inline int case_ignore_strncmp(const QString& s1, const QString& s2, int n) {
 
 int str_match(const char* str, const char* match);
 int case_ignore_str_match(const char* str, const char* match);
-char* strenquote(const char* str, const char quot_char);
+QString strenquote(const QString& str, const QChar quot_char);
 
 char* strsub(const char* s, const char* search, const char* replace);
 char* gstrsub(const char* s, const char* search, const char* replace);
diff --git a/gpsbabel/style/deprecated/sportsim.style b/gpsbabel/style/deprecated/sportsim.style
new file mode 100644 (file)
index 0000000..a029a99
--- /dev/null
@@ -0,0 +1,33 @@
+# gpsbabel XCSV style file
+#
+# Format: Sportsim track files
+# Author: Olaf Klein
+#   Date: 07/05/2006
+#
+DESCRIPTION    Sportsim track files (part of zipped .ssz files) 
+EXTENSION      txt
+DATATYPE       TRACK
+
+#
+# FILE LAYOUT DEFINITIIONS:
+#
+FIELD_DELIMITER                SEMICOLON
+RECORD_DELIMITER       CRNEWLINE
+BADCHARS               TAB
+
+#
+# FILE HEADER
+#
+PROLOGUE       SportsimVersion:01
+PROLOGUE       \#Sportsim TrackFile
+
+#
+# INDIVIDUAL DATA FIELDS:
+#
+IFIELD INDEX, "", "%05d"
+IFIELD CONSTANT, "0", "%s"
+IFIELD LAT_DECIMAL, "", "%f"
+IFIELD LON_DECIMAL, "", "%f"
+IFIELD ALT_FEET, "", "%.f"
+IFIELD TIMET_TIME, "", "%ld"
+IFIELD CONSTANT, ";", "%s"
diff --git a/gpsbabel/style/sportsim.style b/gpsbabel/style/sportsim.style
deleted file mode 100644 (file)
index a029a99..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-# gpsbabel XCSV style file
-#
-# Format: Sportsim track files
-# Author: Olaf Klein
-#   Date: 07/05/2006
-#
-DESCRIPTION    Sportsim track files (part of zipped .ssz files) 
-EXTENSION      txt
-DATATYPE       TRACK
-
-#
-# FILE LAYOUT DEFINITIIONS:
-#
-FIELD_DELIMITER                SEMICOLON
-RECORD_DELIMITER       CRNEWLINE
-BADCHARS               TAB
-
-#
-# FILE HEADER
-#
-PROLOGUE       SportsimVersion:01
-PROLOGUE       \#Sportsim TrackFile
-
-#
-# INDIVIDUAL DATA FIELDS:
-#
-IFIELD INDEX, "", "%05d"
-IFIELD CONSTANT, "0", "%s"
-IFIELD LAT_DECIMAL, "", "%f"
-IFIELD LON_DECIMAL, "", "%f"
-IFIELD ALT_FEET, "", "%.f"
-IFIELD TIMET_TIME, "", "%ld"
-IFIELD CONSTANT, ";", "%s"
diff --git a/gpsbabel/testo.d/deprecated/sportsim.test b/gpsbabel/testo.d/deprecated/sportsim.test
new file mode 100644 (file)
index 0000000..9779cbc
--- /dev/null
@@ -0,0 +1,6 @@
+#
+# Sportsim style-sheet
+#
+rm -f ${TMPDIR}/sportsim*
+gpsbabel -i gpx -f ${REFERENCE}/expertgps.gpx -x nuketypes,waypoints,routes -o sportsim -F ${TMPDIR}/sportsim.txt
+compare ${REFERENCE}/track/sportsim-sample.txt ${TMPDIR}/sportsim.txt
diff --git a/gpsbabel/testo.d/sportsim.test b/gpsbabel/testo.d/sportsim.test
deleted file mode 100644 (file)
index 9779cbc..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#
-# Sportsim style-sheet
-#
-rm -f ${TMPDIR}/sportsim*
-gpsbabel -i gpx -f ${REFERENCE}/expertgps.gpx -x nuketypes,waypoints,routes -o sportsim -F ${TMPDIR}/sportsim.txt
-compare ${REFERENCE}/track/sportsim-sample.txt ${TMPDIR}/sportsim.txt
index 31e6ab7e57726d64e9c1a15d0e0a58c6285d1a95..5a9813f4b25a28d35ca56b8766459ff07adb595e 100644 (file)
@@ -1247,41 +1247,21 @@ unicsv_fatal_outside(const waypoint* wpt)
         gt_get_mps_grid_longname(unicsv_grid_idx, MYNAME));
 }
 
-static void
-unicsv_print_str(const char* str)
-{
-  if (str && *str) {
-    char* cout, *cx;
-
-    cout = strenquote(str, UNICSV_QUOT_CHAR);
-
-    while ((cx = strstr(cout, "\r\n"))) {
-      memmove(cx, cx + 1, strlen(cx));
-      *cx++ = ',';
-      lrtrim(cx);
-    }
-    while ((cx = strchr(cout, '\r'))) {
-      *cx++ = ',';
-      lrtrim(cx);
-    }
-    while ((cx = strchr(cout, '\n'))) {
-      *cx++ = ',';
-      lrtrim(cx);
-    }
-
-    gbfprintf(fout, "%s%s", unicsv_fieldsep, cout);
-    xfree(cout);
-  } else {
-    gbfputs(unicsv_fieldsep, fout);
-  }
-}
-
 static void
 unicsv_print_str(const QString& s)
 {
-  char* t = xstrdup(s.toUtf8().data());
-  unicsv_print_str(t);
-  xfree(t);
+  gbfputs(unicsv_fieldsep, fout);
+  QString t;
+  if (!s.isEmpty()) {
+    t = strenquote(s, UNICSV_QUOT_CHAR);
+    // I'm not sure these three replacements are necessary; they're just a 
+    // slavish re-implementation of (what I think) the original C code 
+    // was doing.
+    t.replace("\r\n", ",");
+    t.replace("\r", ",");
+    t.replace("\n", ",");
+  }
+  gbfputs(t.trimmed(), fout);
 }
 
 #if NEW_STRINGS
@@ -1500,16 +1480,15 @@ unicsv_waypt_disp_cb(const waypoint* wpt)
     break;
 
   case grid_lat_lon_dms: {
-    char* sep, *tmp;
+    char* sep;
+    QString tmp;
     cout = pretty_deg_format(lat, lon, 's', unicsv_fieldsep, 0);
     sep = strchr(cout, ',');
     *sep = '\0';
     tmp = strenquote(cout, UNICSV_QUOT_CHAR);
-    gbfprintf(fout, "%s%s", tmp, unicsv_fieldsep);
-    xfree(tmp);
+    gbfprintf(fout, "%s%s", CSTR(tmp), unicsv_fieldsep);
     tmp = strenquote(sep+1, UNICSV_QUOT_CHAR);
     gbfputs(tmp, fout);
-    xfree(tmp);
   }
   break;
 
index 741d6badb0e887b3771e657f592976ec969f1973..d78f59869352da464485510d88b67e8f9d3345da 100644 (file)
@@ -563,36 +563,20 @@ case_ignore_str_match(const char* str, const char* match)
   return res;
 }
 
-char*
-strenquote(const char* str, const char quot_char)
-{
-  int len;
-  const char* cin;
-  char* cout;
-  char* tmp;
-
-  if (str == NULL) {
-    cin = "";
-  } else {
-    cin = (char*)str;
-  }
-
-  len = strlen(cin);
-  cout = tmp = (char*) xmalloc((len * 2) + 3);
-
-  *cout++ = quot_char;
-  while (*cin) {
-    *cout++    = *cin;
-    if (*cin++ == quot_char) {
-      *cout++  = quot_char;
-    }
-  }
-  *cout++ = quot_char;
-  *cout = '\0';
-
-  cout = xstrdup(tmp);
-  xfree(tmp);
-  return cout;
+// for ruote_char = "
+// make str = blank into nothing
+// make str = foo into "foo"
+// make str = foo"bar into "foo""bar"
+// No, that doesn't seem obvious to me, either...
+
+QString
+strenquote(const QString& str, const QChar quot_char)
+{
+  QString replacement = QString("%1%1").arg(quot_char);
+  QString t = str;
+  t.replace(quot_char, replacement);
+  QString r = quot_char + t + quot_char;
+  return r;
 }
 
 void
index b650d5b75f448681d1c8231a81c484e82b761a42..04301549f7ab4b628c6c985f015a949b8911aeb6 100644 (file)
@@ -78,7 +78,7 @@ hash (register const char *str, register unsigned int len)
       249, 249, 249, 249, 249, 249, 249, 249, 249, 249,
       249, 249, 249, 249, 249, 249
     };
-  register int hval = len;
+  register int hval = (int)len;
 
   switch (hval)
     {
index 4370151e1840a09aadfd15541043537c6d29b5be..7e054628a6f781d3ef2ea8c9fa38a4dbb5d58ce1 100644 (file)
@@ -186,9 +186,8 @@ static void
 xol_write_string(const char* name, const char* str)
 {
   if (str && *str) {
-    char* temp = strenquote(str, '"');
-    gbfprintf(fout, " %s=%s", name, temp);
-    xfree(temp);
+    QString temp = strenquote(str, '"');
+    gbfprintf(fout, " %s=%s", name, CSTR(temp));
   }
 }